<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小账单物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82882996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\approval;

use think\App;
use app\controller\property\Base;
use app\common\repositories\work\WorkExpendApplyRepository;
use app\common\repositories\approval\ApprovalFlowRepository;
use app\common\repositories\approval\ApprovalConfigRepository;

class ApprovalFlow extends Base
{

    protected $repository;

    public function __construct(App $app, ApprovalFlowRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
        $this->assign([
            'printAuth' => property_auth('propertyApprovalFlowPrint'),
            'expendApplyAuth' => property_auth('propertyWorkExpendApplyAdd'),
            'editAuth' => property_auth('propertyApprovalFlowSubmitEdit'),
            'delAuth' => property_auth('propertyApprovalFlowSubmitDel'),
        ]);
    }

    protected function commonParams()
    {
        /** @var ApprovalConfigRepository $configRepository */
        $configRepository = app()->make(ApprovalConfigRepository::class);
        $flowTypeData = $configRepository::FLOW_TYPE_B;
        $this->assign([
            'flowTypeData' => $flowTypeData
        ]);
    }

    /**
     * 我提交审批的
     * @return string|\think\response\Json
     * @throws \Exception
     *
     * @author Amos
     * 2024-05-21 21:13
     */
    public function isubmitList()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'property_id' => '',
                'day_time' => '',
                'keywords' => ''
            ]);
            $where['is_type'] = [2, 3];
            [$page, $limit] = $this->getPage();
            $where['submit_uid'] = $this->request->userId;
            $data = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $data['list'], 'count' => $data['count']]);
        }
        $this->commonParams();
        return $this->fetch('approval/flow/isubmit_list');
    }


    /**
     * 删除我提交的
     * @return string|\think\response\Json|\think\response\View
     *
     * @author Amos
     * 2024-05-24 08:58
     */
    public function isubmitDel()
    {
        $ids = (array)$this->request->param('ids');
        try {
            $data = $this->repository->batchDelete($ids, $this->request->userId);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            exception_log('删除我提交的申请失败', $e);
            return $this->error('网络失败');
        }
    }

    /**
     *  编辑我提交的数据
     * @return string|\think\response\Json|\think\response\View
     *
     * @author Amos
     * 2024-05-24 18:03
     */
    public function isubmitEdit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $flowInfo = $this->repository->get($id);
        if (!$flowInfo) {
            return $this->error('信息错误');
        }
        if ($flowInfo['flow_type'] == 'expend_apply') {
            ## 报销流程
            /** @var WorkExpendApplyRepository $workExpendApplyRepository */
            $workExpendApplyRepository = app()->make(WorkExpendApplyRepository::class);
            $info = $workExpendApplyRepository->get($flowInfo['source_id']);
            if (!$info) {
                return $this->error('信息错误');
            }
            return $this->fetch('work/expend/edit_info', [
                'info' => $info
            ]);
        }


    }


    /**
     *  打印
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-05-24 08:43
     */
    public function printInfo()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        $res = $this->repository->incField($info['id'], 'print_num');
        if ($res) {
            return $this->success('提交成功');
        } else {
            return $this->error('提交失败');
        }
    }
}